import Tpl from './index.ejs';
import AreaManageTpl from './areaManage.ejs';
import AreaAddTpl from './areaAdd.ejs';
import CityManageTpl from './cityManage.ejs';
import CityAddTpl from './cityAdd.ejs';
import TerminalManageTpl from './terminalManage.ejs';
import TerminalAddTpl from './terminalAdd.ejs';
import StationManageTpl from './stationManage.ejs';
import StationAddTpl from './stationAdd.ejs';
import ModelManageTpl from './modelManage.ejs';
import ModelAddTpl from './modelAdd.ejs';
import PinRuleTpl from './pinRule.ejs';
import PinRuleAddTpl from './pinRuleAdd.ejs';
import ZhuanRuleTpl from './zhuanRule.ejs';
import ZhuanRuleAddTpl from './zhuanRuleAdd.ejs';

import StationListTpl from './stationList.ejs';
import CityListTpl from './cityList.ejs';
import TerminalListTpl from './terminalList.ejs';

import Tools from '../tools/tools.js';

class Deploy {

	init ($wrap, router) {
		let self = this;

		this.render($wrap, router, function () {

			$('.ui.checkbox').checkbox();
	        $('.dropdown').dropdown();

			self.$listWrap = $('#listWrap');
			self.reqData = {};

			self.bindEvent();
		});
	}

	bindEvent () {
		let self = this;

		$('#areaAddBtn').on('click', function () {
			self.areaSubmitForm();
		});

		$('#cityAddBtn').on('click', function () {
			self.citySubmitForm();
		});

		$('#cityCheckBtn').on('click', function () {
			self.cityCheckForm();
		});

		$('#modelAddBtn').on('click', function () {
			self.modelSubmitForm();
		});

		$('#stationAddBtn').on('click', function () {
			self.stationSubmitForm();
		});

		$('#stationCheckBtn').on('click', function () {
			self.stationCheckForm();
		});

		$('#terminalAddBtn').on('click', function () {
			self.terminalSubmitForm();
		});

		$('#terminalCheckBtn').on('click', function () {
			self.terminalCheckForm();
		});

		$('#provinceInput').on('change', function () {
			self.getCity($(this));
		});
	}

	getCity ($ele) {
		let self = this;

		$.ajax({
			url: '/city/constant/query',
			method: 'post',
			data: JSON.stringify({
				paged: true,
				pageNo: 1,
				pageSize: 10,
				level: 3,
				parentId: $ele.data('value')
			}),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					let htmlArr = [];

					data.data.forEach(function (item) {

						htmlArr.push('<div class="item" data-value="' + item.id + '">' + item.name + '</div>');
					})

					$('#cityMenu').html(htmlArr.join(''));
				}
			}
		});
	}

	areaSubmitForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10
		}, Tools.getFromData());

        $.ajax({
			url: '/service/regin/add',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('添加成功');
				}
				else {
					alert('添加失败');
				}
			}
        });
	}

	citySubmitForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10
		}, Tools.getFromData());

        $.ajax({
			url: '/city/info/add',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('添加成功');
				}
				else {
					alert('添加失败');
				}
			}
        });
	}

	cityCheckForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10
		}, Tools.getFromData());

        $.ajax({
			url: '/city/info/list',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(CityListTpl(data));
				}
			}
        });
	}

	modelSubmitForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10
		}, Tools.getFromData());

        $.ajax({
			url: '/car/models/add',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('添加成功');
				}
				else {
					alert('添加失败');
				}
			}
        });
	}

	stationSubmitForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: '1'
		}, Tools.getFromData());

        $.ajax({
			url: '/terminal/info/add',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('添加成功');
				}
				else {
					alert('添加失败');
				}
			}
        });
	}

	stationCheckForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
			pageNo: 1,
			pageSize: 10,
			type: '1'
		}, Tools.getFromData());

		$.ajax({
			url: '/terminal/info/queryByCity',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(StationListTpl(data));
				}
			}
		});
	}

	terminalSubmitForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
        	pageNo: 1,
        	pageSize: 10,
			type: '2'
		}, Tools.getFromData());

        $.ajax({
			url: '/terminal/info/add',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					alert('添加成功');
				}
				else {
					alert('添加失败');
				}
			}
        });
	}

	terminalCheckForm () {
		let self = this;

		self.reqData = $.extend({
			paged: true,
			pageNo: 1,
			pageSize: 10,
			type: '2'
		}, Tools.getFromData());

		$.ajax({
			url: '/terminal/info/queryByCity',
			method: 'post',
			data: JSON.stringify(self.reqData),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					self.$listWrap.html(TerminalListTpl(data));
				}
			}
		});
	}

	render ($wrap, router, callback) {
		let self = this;

		if (!router) {
            $wrap.html(Tpl());
        }
        else {
            switch (router) {
               	case 'areaManage':

					$.ajax({
						url: '/service/regin/list',
						method: 'post',
						data: JSON.stringify({
							paged: true,
				        	pageNo: 1,
				        	pageSize: 10
						}),
						contentType: 'application/json',
						dataType: 'json',
						success: function (data) {

							if (data && data.message === 'SUCCESS') {

								$wrap.html(AreaManageTpl(data));
								callback();
							}
						}
			        });
               		break;
                case 'areaAdd':

					Tools.getCity(function (data) {
						$wrap.html(AreaAddTpl(data));
						callback();
					});
               		break;
                case 'cityManage':
                    $wrap.html(CityManageTpl());
					callback();
                    break;
                case 'cityAdd':

					$.ajax({
						url: '/city/constant/query',
						method: 'post',
						data: JSON.stringify({
							paged: true,
							pageNo: 1,
							pageSize: 10,
							level: 2,
							parentId: 1
						}),
						contentType: 'application/json',
						dataType: 'json',
						success: function (data) {

							if (data && data.message === 'SUCCESS') {

								$wrap.html(CityAddTpl(data));
								callback();
							}
						}
					});
                    break;
                case 'terminalManage':

					Tools.getCity(function (data) {
						$wrap.html(TerminalManageTpl(data));
						callback();
					});
               		break;
                case 'terminalAdd':

					Tools.getCity(function (data) {
						$wrap.html(TerminalAddTpl(data));
						callback();
					});
               		break;
                case 'stationManage':

					Tools.getCity(function (data) {
						$wrap.html(StationManageTpl(data));
						callback();
					});
                    break;
                case 'stationAdd':

					Tools.getCity(function (data) {
						$wrap.html(StationAddTpl(data));
						callback();
					});
                    break;
                case 'modelManage':

					$.ajax({
						url: '/car/models/queryCarModel',
						method: 'post',
						data: JSON.stringify({}),
						contentType: 'application/json',
						dataType: 'json',
						success: function (data) {

							if (data && data.message === 'SUCCESS') {

								$wrap.html(ModelManageTpl(data));
								callback();
							}
						}
					});
                    break;
                case 'modelAdd':
                    $wrap.html(ModelAddTpl());
					callback();
                    break;
				case 'pinRule':
                    $wrap.html(PinRuleTpl());
					callback();
                    break;
                case 'pinRuleAdd':
                    $wrap.html(PinRuleAddTpl());
					callback();
                    break;
                case 'zhuanRule':
                    $wrap.html(ZhuanRuleTpl());
					callback();
                    break;
                case 'zhuanRuleAdd':
                    $wrap.html(ZhuanRuleAddTpl());
					callback();
                    break;
               	default:
               		break;
            }
        }
	}
}

export default new Deploy();
